package co.edu.udem.common.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

/**
 * @author sgranada
 * 
 */
public class StreamUtilities {

	/** quotes character */
	private static final String DOUBLE_QUOTES = "\"";

	/** quotes character */
	private static final String COMMA = ", ";

	/** Token separator */
	public static final String TOKEN = System.getProperty("line.separator");

	/**
	 * GZIP Decompress method.
	 * 
	 * @param compressed
	 * @return
	 * @throws IOException
	 */
	public static String decompressGzip(byte[] compressed) throws IOException {
		final int BUFFER_SIZE = 32;
		ByteArrayInputStream is = new ByteArrayInputStream(compressed);
		GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
		StringBuilder string = new StringBuilder();
		byte[] data = new byte[BUFFER_SIZE];
		int bytesRead;
		while ((bytesRead = gis.read(data)) != -1) {
			string.append(new String(data, 0, bytesRead));
		}
		gis.close();
		is.close();
		return string.toString();
	}

	/**
	 * Add value to format.
	 * 
	 * @param atributte
	 * @param addComma
	 * @return
	 */
	public static String appendValueFormat(Object atributte, Boolean addComma) {
		StringBuilder result = new StringBuilder("");
		result.append(DOUBLE_QUOTES);
		result.append(atributte);
		result.append(DOUBLE_QUOTES);
		if (addComma) {
			result.append(COMMA);
		}
		return result.toString();
	}
}
